package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.ThreadlocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStoreService;
    @Override
    public ResponseResult uploadPicture(MultipartFile file) {

        log.info("上传图片开始");

        if (file == null || file.getSize() <= 0){
            log.error("参数错误");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmMaterial wmMaterial = new WmMaterial();
        WmUser user = ThreadlocalUtil.getUser();
        wmMaterial.setUserId(user.getId());
        log.info("构建返回值DATA：{}", wmMaterial);

        try {
            String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String filename = UUID.randomUUID().toString() + extension;
            String imgFilePath = fileStoreService.uploadImgFile("", filename, file.getInputStream());
            log.info("上传图片成功:{}", imgFilePath);
            wmMaterial.setUrl(imgFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("上传图片失败");
        }

        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());

        log.info("保存图片信息开始：{}", wmMaterial);

        boolean saved = save(wmMaterial);
        log.info("保存图片信息结束：{}", saved);
        return ResponseResult.okResult(wmMaterial);

    }

    @Override
    public ResponseResult findList(WmMaterialDto wmMaterialDto) {

        wmMaterialDto.checkParam();
        Page page = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据用户
        lambdaQueryWrapper.eq(WmMaterial::getUserId, ThreadlocalUtil.getUser().getId());
        log.info("userId:{}", ThreadlocalUtil.getUser().getId());

        //是否收藏
        if (wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1){
                lambdaQueryWrapper.eq(WmMaterial::getIsCollection, wmMaterialDto.getIsCollection());
        }
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        Page<WmMaterial> pageWmMaterial = page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(wmMaterialDto.getPage(), wmMaterialDto.getSize(), (int) pageWmMaterial.getTotal());
        pageResponseResult.setData(pageWmMaterial.getRecords());
        return pageResponseResult;
    }
}
